const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            mcategory: [],
            qualification: [],
            childDataForm: [{
                Children: {
                    merchant_qualification: [],
                    merchant_business_mapping_blue_plate: [],
                }
            }],
            dataForm: {
                County_Nubmer: '232723',
            },
            mcategory1: [],
            personal_information: [],
            mapping_blue_plate: [],
            childDataForm1: [{}],
            room_type: [],
            image_type: [],
            image_category: [],
            window_type: [],
            room_facility_type: [],
            blue_plate: [],
            tabs_action: {},
            select_blue_plate: {},
            showComponent: true,
            showblueplate: true,
            drag: false,
            city: [],
            city1: [],
        };
    },
    mounted() {
        this.getQualification();
        this.getCategory();
        if (this.action == 'Add') {
            this.changeCity('232723')
        }
        this.$nextTick(() => { //拖拽排序
            var $ul = document.querySelector('#childDataForm1');
            $ul.addEventListener('selectstart', function (e) {
                e.preventDefault();
            })
            this.Sortable = Sortable.create($ul, {
                handle: '.el-descriptions',
                draggable: '.el-descriptions',
                animation: 150,
                onUpdate: (event) => {
                    let newIndex = event.newIndex, oldIndex = event.oldIndex;
                    $li = $ul.children[newIndex], $oldLi = $ul.children[oldIndex]
                    //先删除移动的节点, 再插入移动的节点到原有节点，还原了移动的操作
                    $ul.removeChild($li)
                    if (newIndex > oldIndex) {
                        $ul.insertBefore($li, $oldLi)
                    } else {
                        $ul.insertBefore($li, $oldLi.nextSibling)
                    }
                    //通过改变vue遍历数组移动节点
                    let item = this.childDataForm1[oldIndex];
                    this.childDataForm1.splice(oldIndex, 1);
                    this.childDataForm1.splice(newIndex, 0, item);
                }
            })
            this.Sortable.option('disabled', !this.drag);
        })
    },
    computed: {
        tabsRoomType() {
            let obj = {};
            this.room_type.forEach(item => {
                console.log(item.Blue_Plate_SerialNumber);
                if (obj[item.Merchant_Category_SerialNumber]) {
                    if (obj[item.Merchant_Category_SerialNumber][item.Blue_Plate_SerialNumber]) {
                        obj[item.Merchant_Category_SerialNumber][item.Blue_Plate_SerialNumber].push(item);
                    } else {
                        obj[item.Merchant_Category_SerialNumber][item.Blue_Plate_SerialNumber] = [item];
                    }
                } else {
                    obj[item.Merchant_Category_SerialNumber] = new Object();
                    obj[item.Merchant_Category_SerialNumber][item.Blue_Plate_SerialNumber] = [item];
                    if (!this.tabs_action[item.Merchant_Category_SerialNumber]) {
                        this.tabs_action[item.Merchant_Category_SerialNumber] = item.Blue_Plate_SerialNumber;
                    }
                }
            })
            if (this.room_type.length == 0) {
                this.tabs_action = {};
            }
            console.log(obj);
            return obj
        }
    },
    methods: {
        success(file) {
            const that = this;
            const formData = new FormData();
            formData.append('file', file, file.name);
            let oFileReader = new FileReader();
            oFileReader.readAsDataURL(file);
            oFileReader.onload = async function (e) {
                $.ajax({
                    headers: requestHeaders("JSON"),
                    url: `${mh}/clientOperation/OCR_BusinessLicense`,
                    type: "post",
                    data: e.target.result.replace('data:image/jpeg;', ''),
                    success: (res) => {
                        if (res.words_result) {
                            that.dataForm.Merchant_Name = res.words_result['单位名称'].words;
                            that.dataForm.Legal_Person = res.words_result['法人'].words;
                            that.dataForm.Business_License = res.words_result['社会信用代码'].words;
                            that.dataForm.Merchant_Address = res.words_result['地址'].words;
                            that.dataForm.Merchant_Description = res.words_result['经营范围'].words;
                            that.dataForm.Register_Date = res.words_result['核准日期'].words;
                        }
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        },
        parseAddress(address) {
            // 常见行政区划关键词（可根据实际需求扩展）
            const provinceKeywords = ['省', '自治区', '北京市', '天津市', '上海市', '重庆市'];
            const cityKeywords = ['市', '自治州', '盟', '地区'];
            const districtKeywords = ['区', '县', '旗', '自治县'];
            const streetKeywords = ['街道', '镇', '乡', '开发区', '高新区'];
            const villageKeywords = ['村', '社区', '居委会', '小区', '组', '庄', '花园', '大厦'];

            // 初始化结果数组
            const result = ['', '', '', '', '', ''];

            // 1. 提取省（匹配省/直辖市/自治区）
            let province = '';
            for (const keyword of provinceKeywords) {
                const index = address.indexOf(keyword);
                if (index !== -1) {
                    province = address.substring(0, index + keyword.length);
                    result[0] = province;
                    address = address.substring(index + keyword.length).trim();
                    break;
                }
            }

            // 2. 提取市（匹配市/自治州）
            let city = '';
            for (const keyword of cityKeywords) {
                const index = address.indexOf(keyword);
                if (index !== -1) {
                    city = address.substring(0, index + keyword.length);
                    result[1] = city;
                    address = address.substring(index + keyword.length).trim();
                    break;
                }
            }

            // 3. 提取区/县
            let district = '';
            for (const keyword of districtKeywords) {
                const index = address.indexOf(keyword);
                if (index !== -1) {
                    district = address.substring(0, index + keyword.length);
                    result[2] = district;
                    address = address.substring(index + keyword.length).trim();
                    break;
                }
            }

            // 4. 提取街道/镇
            let street = '';
            for (const keyword of streetKeywords) {
                const index = address.indexOf(keyword);
                if (index !== -1 && index < 10) { // 限制匹配位置，避免误匹配详细地址中的关键词
                    street = address.substring(0, index + keyword.length);
                    result[3] = street;
                    address = address.substring(index + keyword.length).trim();
                    break;
                }
            }

            // 5. 提取村/社区
            let village = '';
            for (const keyword of villageKeywords) {
                const index = address.indexOf(keyword);
                if (index !== -1 && index < 15) { // 限制匹配位置
                    village = address.substring(0, index + keyword.length);
                    result[4] = village;
                    address = address.substring(index + keyword.length).trim();
                    break;
                }
            }

            // 剩余部分作为详细地址
            result[5] = address.trim();

            return result;
        },
        changeCity(val) {
            if (val) {
                this.dataForm.Province_Number = val.substring(0, 2) + "0000";
                this.dataForm.City_Number = val.substring(0, 4) + "00";
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/clientOperation/getDistrictTree`,
                    type: "GET",
                    data: {
                        DistrictCode: val
                    },
                    success: (data) => {
                        this.city1 = data[0].children;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            } else {
                this.dataForm.Province_Number = '';
                this.dataForm.City_Number = '';
                this.dataForm.County_Nubmer = '';
                this.dataForm.Town_Number = '';
                this.dataForm.Village_Number = '';
            }
        },
        changeCity1(val) {
            if (val) {
                console.log(val);
                this.dataForm.Town_Number = val.substring(0, 9);
            } else {
                this.dataForm.Town_Number = '';
                this.dataForm.Village_Number = '';
            }
        },
        handledrag() {
            this.drag = !this.drag;
            this.Sortable.option('disabled', !this.drag);
        },
        inputMerchant_Address(val) {
            if (val) {
                $.ajax({
                    url: `${y}/getMapWorldAddressResolutionAction`,
                    type: "get",
                    data: {
                        Address: val
                    },
                    success: (res) => {
                        if (res.Status == 'success') {
                            const bd09Lng = res.Data.lon;
                            const bd09Lat = res.Data.lat;

                            // 转换为WGS-84坐标
                            const wgs84 = bd09ToWGS84(bd09Lng, bd09Lat);
                            // 腾讯2000
                            const gcj02 = bd09ToGCJ02(bd09Lng, bd09Lat);
                            this.dataForm.Merchant_Coords = "[" + wgs84.lng + "," + wgs84.lat + "]";
                            // this.dataForm.Merchant_Coords  = "["+ res.Data.lon +","+res.Data.lat  + "]";
                        }
                    },
                    complete: (XMLHttpRequest) => {

                    },
                    dataType: "json",
                });
            }

        },
        // handeChange(val, item) {
        //     if (val) {
        //         let Merchant_Category = this.mcategory.filter(i => i.Merchant_Category_SerialNumber == val)[0].Merchant_Category;
        //         if (item.Blue_Plate_SerialNumber && item.Blue_Plate_SerialNumber.length > 0 && (Merchant_Category.includes('民宿') || Merchant_Category.includes('酒店') || Merchant_Category.includes('公寓'))) {
        //             if (!this.room_type.some(i => i.Merchant_Category_SerialNumber == val)) {
        //                 item.Blue_Plate_SerialNumber.forEach(Blue_Plate_SerialNumber => {
        //                     this['room_type'].push({
        //                         Merchant_SerialNumber: item.Merchant_SerialNumber,
        //                         Merchant_Category_SerialNumber: val,
        //                         Room_Type_SerialNumber: '',
        //                         Blue_Plate_SerialNumber,
        //                         Row_Status: '0',
        //                         Children: {
        //                             room_type_image: [{
        //                                 Room_Type_SerialNumber: '',
        //                                 Row_Status: '0'
        //                             }],
        //                             room_facility: [{
        //                                 Room_Type_SerialNumber: '',
        //                                 Row_Status: '0'
        //                             }]
        //                         }
        //                     })
        //                 })
        //             }
        //         }
        //     }
        //     if (this.action == 'Add') {
        //         this.room_type = this.room_type.filter(i => this.childDataForm.some(x => x.Merchant_Category_SerialNumber == i.Merchant_Category_SerialNumber))
        //     }
        // },
        changeBluePlate(arr, item) {
            if (arr && arr.length > 0) {
                if (item.Merchant_Category_SerialNumber) {
                    let Merchant_Category = this.mcategory.filter(i => i.Merchant_Category_SerialNumber == item.Merchant_Category_SerialNumber)[0].Merchant_Category;
                    if (Merchant_Category.includes('民宿') || Merchant_Category.includes('酒店') || Merchant_Category.includes('宾馆')) {
                        arr.forEach(Blue_Plate_SerialNumber => {
                            this.select_blue_plate[Blue_Plate_SerialNumber] = {
                                Merchant_Alias: '',
                                Contact_Phone: '',
                                blue_plate_house_room: [],
                            }
                            if (!this.room_type.some(i => i.Merchant_Category_SerialNumber == item.Merchant_Category_SerialNumber && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber)) {
                                this['room_type'].push({
                                    Merchant_SerialNumber: item.Merchant_SerialNumber,
                                    Merchant_Category_SerialNumber: item.Merchant_Category_SerialNumber,
                                    Room_Type_SerialNumber: '',
                                    Blue_Plate_SerialNumber,
                                    House_Room_SerialNumber: [],
                                    Row_Status: '0',
                                    Children: {
                                        room_type_image: [{
                                            Room_Type_SerialNumber: '',
                                            Row_Status: '0'
                                        }],
                                        room_facility: [{
                                            Room_Type_SerialNumber: '',
                                            Row_Status: '0'
                                        }],
                                        room_type_detail: []
                                    }
                                })
                            }
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${mh}/generalBusiness/generalQueryByTableName`,
                                method: "post",
                                data: {
                                    Data: 'blue_plate_house_room',
                                    Condition: "Row_Status='0' AND Blue_Plate_SerialNumber = '" + Blue_Plate_SerialNumber + "'",
                                },
                                success: (data) => {
                                    this.select_blue_plate[Blue_Plate_SerialNumber].blue_plate_house_room = data;
                                },
                                complete: (XMLHttpRequest) => {
                                    requestPretreatment(XMLHttpRequest)
                                }
                            })
                        })
                    }

                }
            }
            if (this.action == 'Add') {
                this.room_type = this.room_type.filter(i => this.childDataForm.some(x => x.Merchant_Category_SerialNumber == i.Merchant_Category_SerialNumber && x.Blue_Plate_SerialNumber.some(z => z == i.Blue_Plate_SerialNumber)));
            }
        },
        changehouse_room(arr, item) {
            if (this.action == 'Add') {
                item.Children.room_type_detail = [];
                arr.forEach(House_Room_SerialNumber => {
                    item.Children.room_type_detail.push({
                        House_Room_SerialNumber,
                    });
                })
            }
        },
        handleTabsEdit(Blue_Plate_SerialNumber, Merchant_Category_SerialNumber) {
            this.room_type = this.room_type.filter(i => !(i.Merchant_Category_SerialNumber == Merchant_Category_SerialNumber && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber));
            let obj = this.childDataForm.filter(i => i.Merchant_Category_SerialNumber == Merchant_Category_SerialNumber)[0];
            obj.Blue_Plate_SerialNumber = obj.Blue_Plate_SerialNumber.filter(i => i != Blue_Plate_SerialNumber);
        },
        getCategory() {
            $.ajax({
                url: `${absolutepath()}api/Districtjl.json`,
                method: "get",
                success: (data) => {
                    data.forEach(item => {
                        item.children.forEach(ite => {
                            ite.children.forEach(it => {
                                it.children = undefined
                            })
                        })
                    })
                    this.city = data;
                }
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'merchant_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.mcategory = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'merchant_category',
                    NodeCode: "",
                },
                success: (data) => {
                    this.mcategory1 = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'image_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.image_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'image_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.image_category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'room_facility_type',
                    NodeCode: "",
                },
                success: (data) => {
                    this.room_facility_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'window_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.window_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    console.log(data);
                    this.personal_information = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.blue_plate = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getQualification() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'merchant_qualification_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.qualification = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handleAdd(formName) {
            if (this.action === 'Add') {
                if (formName == 'childDataForm') {
                    this[formName].push({
                        Children: {
                            merchant_qualification: [],
                            merchant_business_mapping_blue_plate: [],
                        }
                    })
                } else {
                    this[formName].push({})
                }
            } else if (this.action === 'Modify') {
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                let lJSONObject_Data = new Object();
                lJSONObject_Data[ls_primary_key] = this.nodeKey;
                if (formName == 'childDataForm') {
                    lJSONObject_Data['Children'] = { merchant_qualification: [], merchant_business_mapping_blue_plate: [] }
                }
                this[formName].push(lJSONObject_Data);
            }
        },
        handleRemove(formName, min = 1) {
            if (this.action === 'Add') {
                if (this[formName].length > min) {
                    this[formName].pop();
                    if (formName === 'childDataForm') {
                        this.room_type = this.room_type.filter(i => this.childDataForm.some(x => x.Merchant_Category_SerialNumber == i.Merchant_Category_SerialNumber))
                    }
                } else {
                    this.$message({
                        message: '不能在删除了',
                        type: 'warning'
                    })
                }
            } else if (this.action === 'Modify') {
                if (this[formName].filter(item => item.Row_Status != '1').length > min) {
                    let i = this[formName].filter(item => item.Row_Status != '1').length - 1
                    let obj = this[formName].filter(item => item.Row_Status != '1')[i]
                    if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                        this[formName].pop();
                    } else {
                        this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning',
                        }).then(() => {
                            this[formName].filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                        }).catch(() => {
                            this.$message({
                                type: 'warning',
                                message: '已取消删除'
                            });
                        });
                    }
                } else {
                    this.$message({
                        message: '不能在删除了',
                        type: 'warning'
                    })
                }
            }
        },
        //孙表
        handleAdd1(row) {
            if (this.action === 'Add') {
                row.Children.merchant_qualification.push({
                    Merchant_Business_Scope_SerialNumber: '',
                    Row_Status: '0',
                });
            } else if (this.action === 'Modify') {
                row.Children.merchant_qualification.push({
                    Merchant_Business_Scope_SerialNumber: row.Merchant_Business_Scope_SerialNumber,
                    Row_Status: '0',
                });
            }
        },
        handleRemove1(row) {
            if (this.action === 'Add') {
                if (row.Children.merchant_qualification.length > 0) {
                    row.Children.merchant_qualification.pop();
                } else {
                    this.$message({
                        message: '不能在删除了',
                        type: 'warning'
                    })
                }
            } else if (this.action === 'Modify') {
                if (row.Children.merchant_qualification.filter(item => item.Row_Status != '1').length > 1) {
                    let i = row.Children.merchant_qualification.filter(item => item.Row_Status != '1').length - 1
                    let obj = row.Children.merchant_qualification.filter(item => item.Row_Status != '1')[i]
                    if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                        row.Children.merchant_qualification.pop();
                    } else {
                        this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning',
                        }).then(() => {
                            row.Children.merchant_qualification.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                        }).catch(() => {
                            this.$message({
                                type: 'warning',
                                message: '已取消删除'
                            });
                        });
                    }
                } else {
                    this.$message({
                        message: '不能在删除了',
                        type: 'warning'
                    })
                }
            }
        },
        handleAddRoom(item, formName) {
            item.Children[formName].push({
                Room_Type_SerialNumber: item.Room_Type_SerialNumber,
                Row_Status: '0',
            });
        },
        handleRemoveRoom(item, formName) {
            if (item.Children[formName].filter(item => item.Row_Status != '1').length > 1) {
                let i = item.Children[formName].filter(item => item.Row_Status != '1').length - 1
                let obj = item.Children[formName].filter(item => item.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    item.Children[formName].pop();
                } else {
                    this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        item.Children[formName].filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        add_room_type(Blue_Plate_SerialNumber, val) {
            let obj = this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber)[0];
            this.room_type.push({
                Merchant_SerialNumber: obj.Merchant_SerialNumber,
                Merchant_Category_SerialNumber: val,
                Room_Type_SerialNumber: '',
                Blue_Plate_SerialNumber,
                House_Room_SerialNumber: [],
                Row_Status: '0',
                Children: {
                    room_type_image: [{
                        Room_Type_SerialNumber: '',
                        Row_Status: '0'
                    }],
                    room_facility: [{
                        Room_Type_SerialNumber: '',
                        Row_Status: '0'
                    }],
                    room_type_detail: []
                }
            })
        },
        remove_room_type(Blue_Plate_SerialNumber, val) {
            this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1')
            if (this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1').length > 1) {
                let i = this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1').length - 1
                let obj = this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1').pop();
                } else {
                    this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        this.room_type.filter(i => i.Merchant_Category_SerialNumber == val && i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber && i.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }

        },
        getData(nodeKey) {//查询
            const that = this;
            //获取对应子表表单对象
            let lJSONObject_Children = new Array();
            let lJSONObject_ChildData = new Object();
            try {
                //可能涉及多个子表
                document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                    let lJSONObject_ChildData_Options = new Object();
                    lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                    let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                    let ls_ChildDataForm = lJSONObject_ChildData_Options.form;

                    lJSONObject_ChildData[ls_ChildTableName] = ls_ChildDataForm;
                    lJSONObject_Children[ls_ChildTableName] = 'ChildTable';
                })
            } catch (error) { }
            //获得对应表单对象
            let lJSONObject_Data_Options = new Object();
            lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
            let ls_DataForm = lJSONObject_Data_Options.form;
            let ls_TableName = lJSONObject_Data_Options.TableName;
            let ls_primary_key = lJSONObject_Data_Options.primary_key

            let lJSONObject_Data = new Object();
            lJSONObject_Data[ls_TableName] = ls_DataForm;
            lJSONObject_Data = { ...lJSONObject_Data, ...lJSONObject_ChildData };

            let lJSONObject_TableName = new Object();
            lJSONObject_TableName[ls_TableName] = 'MainTable'
            lJSONObject_TableName = { ...lJSONObject_TableName, ...lJSONObject_ChildData };
            for (let key in lJSONObject_TableName) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: key,
                        Condition: `${ls_primary_key}='${nodeKey}'`,
                    },
                    success: (res) => {
                        if (lJSONObject_TableName[key] == 'MainTable') {
                            that[lJSONObject_Data[key]] = res[0];
                        } else {
                            that[lJSONObject_Data[key]] = res;
                        }
                        if (key == 'merchant_business_scope') {
                            res.forEach(item => {
                                item['Children'] = { merchant_qualification: [], merchant_business_mapping_blue_plate: [] };
                                $.ajax({
                                    headers: requestHeaders(),
                                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                                    method: "post",
                                    data: {
                                        Data: 'merchant_qualification',
                                        Condition: `Merchant_Business_Scope_SerialNumber='${item.Merchant_Business_Scope_SerialNumber}'`,
                                    },
                                    success: (res) => {
                                        item['Children'].merchant_qualification = res;
                                        that.showComponent = false;
                                        that.$nextTick(() => (that.showComponent = true))
                                    },
                                    complete: (XMLHttpRequest) => {
                                        requestPretreatment(XMLHttpRequest);
                                    },
                                });
                                $.ajax({
                                    headers: requestHeaders(),
                                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                                    method: "post",
                                    data: {
                                        Data: 'merchant_business_mapping_blue_plate',
                                        Condition: `Merchant_Business_Scope_SerialNumber='${item.Merchant_Business_Scope_SerialNumber}'`,
                                    },
                                    success: (res) => {
                                        item.Blue_Plate_SerialNumber = res.map(i => i.Blue_Plate_SerialNumber);
                                        item['Children'].merchant_business_mapping_blue_plate = res;
                                        that.showblueplate = false;
                                        that.$nextTick(() => (that.showblueplate = true))
                                    },
                                    complete: (XMLHttpRequest) => {
                                        requestPretreatment(XMLHttpRequest);
                                    },
                                });
                            })
                        }
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                });
            }
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'room_type',
                    Condition: `${ls_primary_key}='${nodeKey}' AND Row_Status = '0'`,
                },
                success: (room_type) => {
                    room_type.forEach(item => {
                        item.Children = {
                            room_type_image: [], room_facility: []
                        }
                    })
                    let str =  room_type.map(i => "'" + i.Room_Type_SerialNumber + "'").join(',');
                    console.log(str);
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                        method: "post",
                        data: {
                            Data: JSON.stringify([{
                                tableName: 'room_type_image',
                                Condition: window.btoa(unescape(encodeURIComponent("Room_Type_SerialNumber IN (" + (str ? str : "''") + ")  AND Row_Status = '0'"))),
                            }, {
                                tableName: 'room_facility',
                                Condition: window.btoa(unescape(encodeURIComponent("Room_Type_SerialNumber IN (" + (str ? str : "''") + ")  AND Row_Status = '0'"))),
                            }]),
                        },
                        success: ({ room_type_image, room_facility }) => {
                            room_type.forEach(item => {
                                item.Children.room_type_image = room_type_image.filter(i => i.Room_Type_SerialNumber == item.Room_Type_SerialNumber)
                                item.Children.room_facility = room_facility.filter(i => i.Room_Type_SerialNumber == item.Room_Type_SerialNumber)
                                if (!this.tabs_action[item.Merchant_Category_SerialNumber]) {
                                    this.tabs_action[item.Merchant_Category_SerialNumber] = item.Blue_Plate_SerialNumber;
                                }
                            })
                            this.room_type = room_type;
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".ChildTable").forEach(item => {
                        let lJSONObject_ChildData_Options = new Object();
                        lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                        let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                        let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                        // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                        //建立子表结构体
                        let JSONArray_ChildRecord = new Array();
                        JSONArray_ChildRecord = this[ls_ChildDataForm];
                        if (!this[ls_DataForm].Children) {
                            this[ls_DataForm].Children = new Object();
                        }
                        this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                    })
                } catch (error) {

                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();
                this[ls_DataForm].Children['room_type'] = this.room_type;
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    this[ls_DataForm].Children.merchant_business_scope.forEach(item => {
                        item.Blue_Plate_SerialNumber.forEach(Blue_Plate_SerialNumber => {
                            item.Children.merchant_business_mapping_blue_plate.push({
                                Merchant_Business_Scope_SerialNumber: item.Merchant_Business_Scope_SerialNumber,
                                Blue_Plate_SerialNumber,
                                Merchant_Alias: this.select_blue_plate[Blue_Plate_SerialNumber]?this.select_blue_plate[Blue_Plate_SerialNumber].Merchant_Alias:'',
                                Contact_Phone: this.select_blue_plate[Blue_Plate_SerialNumber]?this.select_blue_plate[Blue_Plate_SerialNumber].Contact_Phone:''
                            })
                        })
                    })
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                } else if (action == 'Modify') {
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let room_type = [], merchant_business_mapping_blue_plate = [];
                    //因为room_type是子表 通过 deal 方法自动将新增的部分提出来，所以需要通过modify获取孙表
                    modify.Children['room_type'].forEach(item => {
                        const { add } = this.deal(item);
                        if (add) {
                            room_type.push(add)
                        }
                    })
                    //因为 merchant_business_mapping_blue_plate 与上面的room_type不同， 该表是在提交时候处理组合，room_type在提交前就已经形成了提交孙表结构。
                    modify.Children['merchant_business_scope'].forEach(item => {
                        item.Blue_Plate_SerialNumber.forEach(Blue_Plate_SerialNumber => {
                            if (!item.Children.merchant_business_mapping_blue_plate.some(i => i.Blue_Plate_SerialNumber == Blue_Plate_SerialNumber)) {
                                merchant_business_mapping_blue_plate.push({
                                    Merchant_Business_Scope_SerialNumber: item.Merchant_Business_Scope_SerialNumber,
                                    Blue_Plate_SerialNumber,
                                })
                            }
                        })
                    })
                    add.Children['merchant_business_scope'].forEach(item => {
                        item.Blue_Plate_SerialNumber.forEach(Blue_Plate_SerialNumber => {
                            item.Children.merchant_business_mapping_blue_plate.push({
                                Merchant_Business_Scope_SerialNumber: item.Merchant_Business_Scope_SerialNumber,
                                Blue_Plate_SerialNumber,
                            })
                        })
                    })
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                    lJSONObject_DataSet['Add']['room_type'] = room_type;
                    lJSONObject_DataSet['Add']['merchant_business_mapping_blue_plate'] = merchant_business_mapping_blue_plate;

                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                    this.loading = false;
                })
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             window.location.href = './index.html'
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         this.loading = false;
                //         requestPretreatment(XMLHttpRequest);
                //     },
                // });
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },
    },
};

const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-select', ylSelect);
app.component('yl-cascader', ylCascader);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
